<?php
define('HOST', '192.168.1.1');
define('USER', 'user');
define('PASS', 'pass');

function db(&$db) {
    if($db) mysql_close($db);
    $db = mysql_connect(HOST, USER, PASS);
    mysql_select_db('db', $db);
    mysql_set_charset('utf8', $db);
}

function r(&$db, $sql) {
    db($db);
    $ret = array();
    if(FALSE !== ($res = mysql_query($sql, $db))) {
        while($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
            $ret[] = $row;
        }
    }
    return $ret;
}

function w(&$db, $sql) {
    db($db);
    if(FALSE !== ($res = mysql_query($sql, $db))) return 1;
    else {
	l(mysql_error($db), -1);
	return 0;
    }
}

function l($str, $good=0) {
    if(strpos(PHP_OS, "WIN") !== FALSE) $str = _str_get_encoding($str, "GBK");
    else {
        if($good==1) $str = "\033[32m".$str."\033[0m";
        else if($good==-1) $str = "\033[31m".$str."\033[0m";
    }
    echo $str."\n";
}

function t($int) {
    return date('Y-m-d H:i:s', $int);
}
